﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Bluebunnie.Library.Transaction.ProjectDataSetTableAdapters;
using Bluebunnie.Extensions;

namespace Bluebunnie.Library.Transaction
{
    /// <summary>
    /// Interaction logic for PurchaseReturn.xaml
    /// </summary>
    public partial class PurchaseReturn : PulseControl
    {
        tbl_purchase_returnTableAdapter adapter = new tbl_purchase_returnTableAdapter();
        tbl_purchase_transactionTableAdapter transAdapter = new tbl_purchase_transactionTableAdapter();
        tbl_supplierTableAdapter suppAdapter = new tbl_supplierTableAdapter();
        tbl_userTableAdapter userAdapter = new tbl_userTableAdapter();
        tbl_itemTableAdapter itemAdapter = new tbl_itemTableAdapter();

        public PurchaseReturn()
        {
            InitializeComponent();

            Save = (sender, e) =>
                {
                    ProjectDataSet dataset = (ProjectDataSet)FindResource("projectDataSet");
                    adapter.Update(dataset.tbl_purchase_return);
                };

            try
            {
                Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Tidak dapat mengambil data. Pastikan server MySQL telah berjalan.\n\nError:\n{0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public override void Refresh()
        {
            base.Refresh();

            ProjectDataSet dataset = (ProjectDataSet)FindResource("projectDataSet");
            adapter.Fill(dataset.tbl_purchase_return);
            transAdapter.Fill(dataset.tbl_purchase_transaction);
            suppAdapter.Fill(dataset.tbl_supplier);
            userAdapter.Fill(dataset.tbl_user);
            itemAdapter.Fill(dataset.tbl_item);

            CollectionViewSource source = (CollectionViewSource)FindResource("tbl_purchase_returnViewSource");
            source.View.MoveCurrentToFirst();
        }
    }
}
